<?php

namespace App\Http\Controllers\wechat\product;

use App\Http\Controllers\Controller;
use App\Models\product\ProductModel;
use App\Models\product\ProductPopularizeModel;
use App\Service\product\ProductApiService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class ProductController extends Controller
{

    /**
     * 获取首页产品列表页面数据
     */
    public function getHomeProductListData(): JsonResponse
    {
        $list[]['product'] = ProductModel::where('deleted_at', 0)
            ->where('status', 1)
            ->orderBy('sort', 'asc')
            ->orderBy('sort_pid', 'asc')
            ->limit(3)->get()->toArray();
        $list[]['product'] = ProductModel::where('deleted_at', 0)
            ->where('status', 1)
            ->orderBy('sort', 'asc')
            ->orderBy('sort_pid', 'asc')
            ->limit(3)->offset(3)->get()->toArray();
        $list[]['product'] = ProductModel::where('deleted_at', 0)
            ->where('status', 1)
            ->orderBy('sort', 'asc')
            ->orderBy('sort_pid', 'asc')
            ->limit(3)->offset(6)->get()->toArray();

        foreach ($list as $k => $v) {
            if (empty($v['product'])) {
                unset($list[$k]);
                continue;
            }
            foreach ($v['product'] as $kk => $vv) {
                $list[$k]['product'][$kk]['live_time'] = Carbon::createFromTimestamp($vv['live_time'])->format('Y-m-d');
                $list[$k]['product'][$kk]['popularize_platform'] = ProductPopularizeModel::whereIn('id', explode(',', $vv['popularize_platform']))->get()->toArray();
            }
        }

        return ApiResponse::apiResponseData(200, '获取首页产品列表页面数据成功', [
            'product_list' => $list,
        ]);
    }


    /**
     * 获取产品广场页面数据
     */
    public function getProductSquareData(Request $request): JsonResponse
    {

        $online_platform = $request->input('online_platform') ?? 1;
        $type = $request->input('type');
        $isMore = $request->input('is_more');

        $result = ProductApiService::instance()->getProductSquareData($online_platform, $type, $isMore);
        if ($result['code'] != 200) {
            return ApiResponse::apiResponseData(4004, '获取产品广场页面数据失败');
        }

        return ApiResponse::apiResponseData(200, '获取产品广场页面数据成功', $result['data']);
    }


    /**
     * 获取产品详情产品介绍页面数据
     */
    public function getProductDetailIntroduceData(Request $request): JsonResponse
    {

        $introduce = Tools::Config('SYSTEM');

        return ApiResponse::apiResponseData(200, '获取产品详情产品介绍页面数据成功', [
            'introduce' => $introduce,
        ]);
    }


    /**
     * 获取产品详情页面数据
     */
    public function getProductDetailData(Request $request): JsonResponse
    {
        $id = $request->input('product_id');
        $popularize_platform = $request->input('popularize_platform');
        $uid = $request->uid;

        $result = ProductApiService::instance()->getProductDetailData($id, $popularize_platform, $uid);
        if ($result['code'] != 200) {
            return ApiResponse::apiResponseData($result['code'], $result['message']);
        }

        // Api检查用户是否律师状态
//        $client = new Client();
//        $res = $client->request('post', '', [
//            'unique_id' => $userInfo->unique_id
//        ]);
//        if (json_decode((string)$res->getBody(), true)['code'] === 200) {
//            $userInfo->is_lawyer = json_decode((string)$res->getBody(), true)['data']['is_lawyer'];
//            UsersModel::where('id', $userInfo->id)->update(['is_lawyer' => $userInfo->is_lawyer]);
//        }

        return ApiResponse::apiResponseData(200, '获取产品详情页面数据成功', $result['data']);
    }


    /**
     * 获取产品介绍列表页面数据
     */
    public function getProductIntroduceListData(Request $request): JsonResponse
    {

        $page = $request->input('page');
        $pageSize = $request->input('page_size');

        $model = ProductModel::with([
                'productSon' => function ($query) {
                    $query->orderBy('popularize_id');
                }
            ])
            ->where('deleted_at', 0);

        $count = $model->count();

        $list = $model->orderBy('sort', 'desc')
            ->orderBy('sort_pid', 'asc')
            ->orderBy('update_at', 'desc')
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->get()->toArray();

        $list = array_map(function ($item) {
            $item['platform'] = ProductPopularizeModel::whereIn('id', explode(',', $item['popularize_platform']))->get()->toArray();
            return $item;
        }, $list);

        return ApiResponse::success('获取产品介绍列表页面数据成功', ['list' => $list, 'total' => $count]);
    }


}
